﻿using System;
using System.Collections.Generic;

namespace CSharp进阶_List排序
{
	//class Item : IComparable, IComparable<Item>
	//{
	//	public int money;
	//	public Item(int money)
	//	{
	//		this.money = money;
	//	}

	//	public int CompareTo(object obj)
	//	{
	//		if (money > (obj as Item).money) return 1;
	//		else return -1;
	//	}

	//	public int CompareTo(Item other)
	//	{
	//		if (money > other.money) return 1;
	//		else return -1;
	//	}

	//	public override string ToString()
	//	{
	//		return money.ToString();
	//	}
	//}
	class Monster
	{
		public int atk;
		public int def;
		public int hp;
		public Monster(int atk, int def, int hp)
		{
			this.atk = atk;
			this.def = def;
			this.hp = hp;
		}
		public override string ToString()
		{
			return "\t" + atk + "\t" + def + "\t" + hp;
		}
	}
	class Item
	{
		public int id;
		public string name;
		public int quality;
		public Item(int id, string name, int quality)
		{
			this.id = id;
			this.name = name;
			this.quality = quality;
		}

		public override string ToString()
		{
			return "\t" + id + "\t" + name + "\t" + quality;
		}
	}
	class Program
	{
		//static int CompareByAtk(Monster a, Monster b)
		//{
		//	if (a.atk < b.atk) return 1;
		//	else return -1;
		//}
		//static int CompareByDef(Monster a, Monster b)
		//{
		//	if (a.def < b.def) return 1;
		//	else return -1;
		//}
		//static int CompareByHp(Monster a, Monster b)
		//{
		//	if (a.hp < b.hp) return 1;
		//	else return -1;
		//}
		static void Main(string[] args)
		{
			//Dictionary<string, int> dict = new Dictionary<string, int>();
			//dict.Add("banana", 3);
			//dict.Add("apple", 5);
			//dict.Add("pair", 1);
			//dict.Add("Sword", 10);
			//Print(dict);
			//List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
			//foreach (KeyValuePair<string, int> kvp in dict)
			//{
			//	list.Add(kvp);
			//}
			//list.Sort((a, b) =>
			//{
			//	return a.Key.CompareTo(b.Key);
			//});
			//Print(list);


			List<Item> items = new List<Item>();
			items.Add(new Item(1, "banana", 3));
			items.Add(new Item(1, "bee", 5));
			items.Add(new Item(2, "pair", 3));
			items.Add(new Item(5, "apple", 6));
			items.Add(new Item(2, "Sword", 5));
			Print(items);
			items.Sort((a, b) =>
			{
				if (a.id != b.id)
				{
					return a.id > b.id ? 1 : -1;
				}
				else if (a.quality != b.quality)
				{
					return a.quality > b.quality ? 1 : -1;
				}
				else
				{
					return a.name.Length > b.name.Length ? 1 : -1;
				}
			});
			Print(items);



			//	List<Monster> monsters = new List<Monster>();
			//	monsters.Add(new Monster(1, 10, 1000));
			//	monsters.Add(new Monster(20, 1, 1));
			//	monsters.Add(new Monster(5, 1, 1));
			//	monsters.Add(new Monster(5, 1, 100));
			//	monsters.Add(new Monster(110, 1, 1));

			//	int select = 0;
			//	do
			//	{
			//		Print(monsters);
			//		Console.WriteLine("------------------------");
			//		Console.WriteLine("\t1.攻击排序");
			//		Console.WriteLine("\t2.防御排序");
			//		Console.WriteLine("\t3.血量排序");
			//		Console.WriteLine("\t4.反转排序");
			//		Console.WriteLine("\t0.退出");
			//		Console.WriteLine("------------------------");
			//		Console.Write("请选择> ");
			//		select = int.Parse(Console.ReadLine());
			//		switch (select)
			//		{
			//			case 1:
			//				monsters.Sort(CompareByAtk);
			//				break;
			//			case 2:
			//				monsters.Sort(CompareByDef);
			//				break;
			//			case 3:
			//				monsters.Sort(CompareByHp);
			//				break;
			//			case 4:
			//				monsters.Reverse();
			//				break;
			//		}
			//		Console.Clear();
			//	} while (select != 0);


			//List<int> l1 = new List<int>();
			//l1.Add(1);
			//l1.Add(3);
			//l1.Add(5);
			//l1.Add(2);
			//l1.Add(4);
			//Print(l1);
			//l1.Sort();
			//Print(l1);

			//List<Item> l2 = new List<Item>();
			//l2.Add(new Item(1));
			//l2.Add(new Item(3));
			//l2.Add(new Item(5));
			//l2.Add(new Item(2));
			//l2.Add(new Item(4));
			//Print(l2);
			//l2.Sort();
			//Print(l2);
			//l2.Sort((a, b) =>
			//{
			//	if (a.money < b.money) return 1;
			//	else return -1;
			//});
			//Print(l2);
			//l2.Sort(delegate (Item a, Item b) 
			//{
			//	if (a.money > b.money) return 1;
			//	else return -1;
			//});
			//Print(l2);
			//l2.Sort(Comp);
			//Print(l2);
		}
		//static int Comp(Item a, Item b)
		//{
		//	if (a.money < b.money) return 1;
		//	else return -1;
		//}
		static void Print<T>(List<T> l)
		{
			Console.WriteLine("----------");
			foreach (T t in l)
			{
				Console.WriteLine(t);
			}
			Console.WriteLine("----------");
		}
		static void Print<K, V>(Dictionary<K, V> l)
		{
			Console.WriteLine("----------");
			
			foreach (KeyValuePair<K, V> t in l)
			{
				//Console.WriteLine("\t" + t.Key + "\t" + t.Value);
				Console.WriteLine(t);
			}

			Console.WriteLine("----------");
		}

	}
}
